package cn.zpro.gen.util;

import cn.zpro.gen.dto.ColumnModelDTO;

import java.util.List;
import java.util.Random;

/**
 * The type Utlis.
 *
 * @author :<a href="mailto:guolongzhang@ebnew.com">张国龙</a>
 * @date :2017-05-25 13:28:21
 */
public class Utlis {

    public static String getLowercaseChar(String str) {
        return str.substring(0, 1).toLowerCase() + str.substring(1);
    }

    public static void main(String[] args) {
        System.out.println(getEntityFun("reserve"));
    }

    /**
     * 根据表的列名称获取生成的Entity列名称
     * @param tableColumn
     * @return
     */
    public static String getEntityName(String tableColumn){
        StringBuffer result = new StringBuffer();
        String[] split = tableColumn.split("_");
        for (int i=0;i<split.length;i++) {
            if(i==0){
                result.append(split[i].toLowerCase());
            }else {
                String str = split[i].toLowerCase();
                result.append(str.substring(0, 1).toUpperCase() + str.substring(1));
            }
        }
        return result.toString();
    }
    public static String getTestEntityCode(List<ColumnModelDTO> list,String modelName){
        StringBuilder code = new StringBuilder();
        code.append(modelName + " "+"entity"+" =new "+modelName + "();\n");
        for (ColumnModelDTO dto:list) {
            code.append("entity.set"+dto.getEntityFunName()+"("+randomValue2(dto.getEntityType())+");\n");
        }
        return code.toString();
    }
    public static String randomValue2(String entityType){
        Random random = new Random();
        if("Long".equals(entityType)){
            return random.nextInt(10000) + "L";
        }else if("Integer".equals(entityType)){
            return random.nextInt(50)+"";
        }else if("String".equals(entityType)){
            int index = random.nextInt(stringRandoms.length);
            return stringRandoms[index];
        }else if("Date".equals(entityType)){
            return "new java.util.Date()";
        }else if("Boolean".equals(entityType)){
            return random.nextBoolean()+"";
        }else if("Double".equals(entityType)){
            return random.nextInt(10000) + "D";
        }

        return entityType;
    }

    private static final String[] stringRandoms = new String[]{"\"test\"", "\"testData\"", "\"测试数据\"", "\"测试\"",
            "\"123456\""};

    public static String getEntityFun(String entityName){
        StringBuffer result = new StringBuffer();
        result.append(entityName.substring(0, 1).toUpperCase() + entityName.substring(1));
        return result.toString();
    }
    public static String getEntityNameType(String tableColumnType){
        tableColumnType = tableColumnType.toLowerCase();
        StringBuffer result = new StringBuffer();
        if("bigint".equals(tableColumnType)){
            result.append("Long");
        }
        if("varchar".equals(tableColumnType)){
            result.append("String");
        }
        if("tinyint".equals(tableColumnType)){
            result.append("Integer");
        }
        if("datetime".equals(tableColumnType)){
            result.append("Date");
        }
        if("mediumtext".equals(tableColumnType)){
            result.append("String");
        }
        if("timestamp".equals(tableColumnType)){
            result.append("Date");
        }

        return result.toString();
    }
}
